# Maintainer: Jelle van der Waa <jelle@archlinux.org>
# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Sebastian Zwierzchowski <seba@angrysoft.ovh>

pkgname=zigbee2mqtt
pkgver=1.33.2
pkgrel=1
pkgdesc='A Zigbee to MQTT bridge'
arch=('loong64' 'x86_64')
url='https://www.zigbee2mqtt.io'
license=('GPL3')
depends=('nodejs-lts-hydrogen') # FS#75807
makedepends=('git' 'typescript' 'npm')
backup=('etc/zigbee2mqtt/configuration.yaml')
optdepends=('mosquitto: MQTT broker')
options=(!emptydirs)
_commit='9996c931e25a0a3e72d9bb1aa89ed9c36eec8a89'
source=(
  "${pkgname}::git+https://github.com/Koenkk/zigbee2mqtt#commit=${_commit}"
  'zigbee2mqtt.service'
  'zigbee2mqtt.sysusers'
  'zigbee2mqtt.tmpfiles'
)
sha256sums=('SKIP'
            '04d58763942cc598ac858f003b174316b695c2a542b2788e50751c3077adf106'
            '3a86716e9036e97d885e9b5f37c7f87d9c2872435e4acf9fc4c9157264cf387b'
            'b8cb53b06176abb09e74e68daee702263f3d471b249c51b1b32d693fc7354fd8')

pkgver() {
  cd "${pkgname}"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "${pkgname}"

  npm install
}

build() {
  cd "${pkgname}"

  NODE_ENV=production npm run build

  npm prune --production
}

package() {
  cd "${pkgname}"

  install -m0755 -d "${pkgdir}/usr/share/webapps/${pkgname}"
  cp -a node_modules dist "${pkgdir}/usr/share/webapps/${pkgname}"

  # FS#80123
  # https://github.com/Koenkk/zigbee2mqtt/blob/51f15bd306db1249e02b0d4fd433d06173ac0e04/docker/Dockerfile#L31
  echo "${_commit}" > "${pkgdir}/usr/share/webapps/${pkgname}/dist/.hash"

  install -m0644 cli.js -t "${pkgdir}/usr/share/webapps/${pkgname}"
  install -m0644 index.js -t "${pkgdir}/usr/share/webapps/${pkgname}"
  install -m0644 package.json -t "${pkgdir}/usr/share/webapps/${pkgname}"

  # sanitize permissions
  find "${pkgdir}/usr" -type d -exec chmod 755 {} +
  chown -R root:root "${pkgdir}"

  install -Dm644 data/configuration.yaml "${pkgdir}/etc/${pkgname}/configuration.yaml"

  install -Dm644 "${srcdir}/${pkgname}.service" "${pkgdir}/usr/lib/systemd/system/${pkgname}.service"
  install -Dm644 "${srcdir}/${pkgname}.sysusers" "${pkgdir}/usr/lib/sysusers.d/${pkgname}.conf"
  install -Dm644 "${srcdir}/${pkgname}.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/${pkgname}.conf"

  # Cleanups x86_64 specific
  if [ "${CARCH}" = "x86_64" ]; then
    local find_dirs=(
    -iwholename '*.github' -o
    -iwholename '*.idea' -o
    -iwholename '*.tscache' -o
    -iwholename '*/man' -o
    -iwholename '*/test' -o
    -iwholename '*/scripts' -o
    -iwholename '*/git-hooks' -o
    -iwholename '*/android-arm' -o
    -iwholename '*/android-arm64' -o
    -iwholename '*/linux-arm64' -o
    -iwholename '*/linux-armvy' -o
    -iwholename '*/linux-armv7' -o
    -iwholename '*/linux-arm' -o
    -iwholename '*/win32-ia32' -o
    -iwholename '*/win32-x64' -o
    -iwholename '*/darwin-x64'
    )

    find ${pkgdir}/usr/share/webapps/${pkgname}/node_modules -type d \( "${find_dirs[@]}" \) -exec rm -rvf {} +
  fi

}
